﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace Sakura.Games.StgFramework.Builder.UI
{
	/// <summary>
	/// 表示伤害类型编辑页面。
	/// </summary>
	public partial class DamageTypeEditPage : UserControl
	{

		/// <summary>
		/// 获取或设置编辑页面要编辑的项目集合。
		/// </summary>
		public IList<DamageType> Items
		{
			get
			{
				return (IList<DamageType>)GetValue(ItemsProperty);
			}
			set
			{
				SetValue(ItemsProperty, value);
			}
		}

		/// <summary>
		/// 标识 <see cref="Items"/> 依赖项属性。
		/// </summary>
		public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(IList<DamageType>), typeof(DamageTypeEditPage), new UIPropertyMetadata(null));

		/// <summary>
		/// 初始化一个页面的新实例。
		/// </summary>
		public DamageTypeEditPage()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 执行删除操作。
		/// </summary>
		private void DeleteSelection()
		{
			var items = from DamageType i in this.MainListBox.SelectedItems
						select i;

			foreach (var i in items.ToArray())
			{
				this.Items.Remove(i);
			}
		}

		/// <summary>
		/// 提示警告并在必要时执行删除操作。
		/// </summary>
		private void OnDelete()
		{
			using (var dialog = new TaskDialog
			{
				Caption = App.Current.GetResourceString("AppTitle"),
				InstructionText = App.Current.GetResourceString("MsgDeleteWarningTitle"),
				Text = App.Current.GetResourceString("MsgDeleteWarning"),
				Cancelable = true,
				Icon = TaskDialogStandardIcon.Warning,
				StandardButtons = TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.No
			})
			{
				switch (dialog.Show())
				{
					case TaskDialogResult.Yes:
						this.DeleteSelection();
						break;
					default:
						break;
				}
			}

		}

		#region 命令绑定

		private void OnAddCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void OnAddExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			var window = new DamageTypeEditWindow();

			if (window.ShowDialog() == true)
			{
				this.Items.Add(window.NewItem);
			}
		}

		private void OnDeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = this.MainListBox.SelectedItems.Count != 0;
		}

		private void OnDeleteExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			this.OnDelete();
		}


		private void OnEditCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void OnEditExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			// 获得参数
			var item = e.Parameter as DamageType;

			var window = new DamageTypeEditWindow();
			window.SetItemData(item);

			if (window.ShowDialog() == true)
			{
				var newItem = window.NewItem;
				item.Name = newItem.Name;
				item.Description = newItem.Description;
			}
		}

		#endregion

	}
}
